package com.example.administrator.pathdemo

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.SystemClock
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    var second = 0
    val handler =  Handler()


    private var runnable:Runnable? = null



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btn.setOnClickListener {

            val timeStr = et_time.text.toString().trim()

            if (timeStr.isNullOrEmpty()){
                return@setOnClickListener
            }

            second = timeStr.toInt()/1000

            pathDemo.startAnim(timeStr.toLong())
//
//            timer.base = SystemClock.elapsedRealtime();//计时器清零
//            val hour = ((SystemClock.elapsedRealtime() - timer.base) / 1000 / 60) as Int
//            timer.format = "0" + hour.toString() + ":%s"
//            timer.start()



            runnable = object :Runnable{
            override fun run() {
                second--
                tvTime.text = getTime(second);
                handler.removeCallbacksAndMessages(null)
                if (second==0){
                    handler.removeCallbacksAndMessages(null)
                    return
                }
                handler.postDelayed(runnable, 1000)
            }
        }
            handler.post(runnable)
        }

    }

    //根据秒数转化为时分秒   00:00:00
    fun getTime(second: Int): String {
        var second = second
        if (second < 10) {
            return "00:0$second"
        }
        if (second < 60) {
            return "00:$second"
        }
        if (second < 3600) {
            val minute = second / 60
            second = second - minute * 60
            if (minute < 10) {
                return if (second < 10) {
                    "0$minute:0$second"
                } else "0$minute:$second"
            }
            return if (second < 10) {
                minute.toString() + ":0" + second
            } else minute.toString() + ":" + second
        }
        val hour = second / 3600
        val minute = (second - hour * 3600) / 60
        second = second - hour * 3600 - minute * 60
        if (hour < 10) {
            if (minute < 10) {
                return if (second < 10) {
                    "0$hour:0$minute:0$second"
                } else "0$hour:0$minute:$second"
            }
            return if (second < 10) {
                "0$hour$minute:0$second"
            } else "0$hour$minute:$second"
        }
        if (minute < 10) {
            return if (second < 10) {
                hour.toString() + ":0" + minute + ":0" + second
            } else hour.toString() + ":0" + minute + ":" + second
        }
        return if (second < 10) {
            (hour + minute).toString() + ":0" + second
        } else (hour + minute).toString() + ":" + second
    }


    override fun onDestroy() {
        super.onDestroy()

        pathDemo.stopAnim()
    }

}
